package com.zoweunion.mechanic.service.base.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.base.PartsManagerDao;
import com.zoweunion.mechanic.dao.order.CGOrderDao;
import com.zoweunion.mechanic.entity.PartsDailyBean;
import com.zoweunion.mechanic.service.base.PartsManagerService;
import com.zoweunion.mechanic.util.CGOrderEntity;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.PartCountEntity;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class PartsManagerServiceImpl implements PartsManagerService {

    @Autowired
    private PartsManagerDao partsManagerDao;
    @Autowired
    private CGOrderDao cGOrderDao;

    @Override
    public PageInfo getPartsDailyStatistics(Map<String, Object> reqmap) throws MyException {
        Integer pageSize = Integer.valueOf(reqmap.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(reqmap.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String, Object>> dailyPartsList = partsManagerDao.getPartsCoutByDaily(reqmap);
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(dailyPartsList);
        return mapPageInfo;
    }

    @Override
    public void getDailyParts() throws MyException {
        Date today = new Date();//获取今天的日期
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = c.getTime();//这是昨天
        List<CGOrderEntity> orderEntities = partsManagerDao.selectOrderPartsByTime2(yesterday);
        try {
            this.orderParts2(orderEntities, yesterday);
        } catch (Exception e) {
            throw new MyException(Constants.CODE_SUCCESS, "定时统计配件异常");
        }
    }

    /**
     * 批量补录数据
     * */
    @Override
    public void getDailyPartsTest() throws MyException {
        for(int i = 178;i>=0;i--) {
            /**
             * 今天的日期
             */
            Date today = new Date();//获取今天的日期
            Calendar c = Calendar.getInstance();
            c.setTime(today);
            c.add(Calendar.DAY_OF_MONTH, -i);
            Date yesterday = c.getTime();//这是昨天
//            String yesterday = "2020-09-24";
            List<CGOrderEntity> orderEntities = partsManagerDao.selectOrderPartsByTime2(yesterday);
            try {
                this.orderParts2(orderEntities, yesterday);
            } catch (Exception e) {
                throw new MyException(Constants.CODE_SUCCESS, "定时统计配件异常");
            }
        }
    }

    @Override
    public List<PartsDailyBean> partsDailyExport(Map<String, Object> reqmap) {
        Date startTime = Orgin.getDate(reqmap, "startTime");
        Date endTime = Orgin.getDate(reqmap, "endTime");
        String sId = Orgin.getString(reqmap, "sId");
        String dictionariesCode = Orgin.getString(reqmap, "dictionariesCode");
        Map<String, Object> map = new HashMap<>();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("sId", sId);
        map.put("dictionariesCode", dictionariesCode);
        List<PartsDailyBean> dailyPartsList = partsManagerDao.getPartsDailyExport(map);
        return dailyPartsList;
    }

    /**
     * 配件列表
     * */
    @Override
    public List<String> getPartsCode(Map<String, Object> reqMap) {
        List<String> partsCode = partsManagerDao.getPartsCode(reqMap);
        return partsCode;
    }

    /**
     * 批量补录数据
     * */
    private void orderParts2(List<CGOrderEntity> orderEntities, Date yesterday) {
        //pid,sid
        orderEntities.parallelStream().forEach(item1 -> {
            List<CGOrderEntity> orderEntities1 =
                    partsManagerDao.selectPartsByPIds2(item1.getPartsId(), item1.getSId(), yesterday);
            //加
            List<CGOrderEntity> entities =
                    orderEntities1.stream().filter(a -> (a.getChangeFlag() == 0 || a.getChangeFlag() == 1))
                            .collect(Collectors.toList());
            int sum = entities.stream().mapToInt(CGOrderEntity::getQuantity).sum();
            //减
            List<CGOrderEntity> entitiess =
                    orderEntities1.stream().filter(a -> a.getChangeFlag() == 2).collect(Collectors.toList());
            int sum1 = entitiess.stream().mapToInt(CGOrderEntity::getQuantity).sum();
            int i = sum - sum1;
            if (i >= 1) {
                cGOrderDao.insertPartsCount(PartCountEntity.builder()
                        .count(i)
                        .s_id(item1.getSId())
                        .create_time(item1.getCreateTime())
                        .parts_id(item1.getPartsId())
                        .build());
            }
        });
    }
}
